﻿Public Class Form1
    Private Encoder As New NetworkTransport
    Private RemoteFileList As New List(Of String)

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim Info As New IPInfo
        NetworkClient1.RemoteIP = Info.IPAddress
        NetworkClient1.RemotePort = 4999
        Timer1.Enabled = True
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        NetworkClient1.Connect()
        NetworkClient1.Send(Encoder.Encode("GetDir", ""))
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        NetworkClient1.Disconnect()
        ListBox1.Items.Clear()
    End Sub

    Private Sub NetworkClient1_DataReceived(ByVal Bytes() As Byte) Handles NetworkClient1.DataReceived
        Encoder.Decode(Bytes)
        If Encoder.ContainRemote Then
            Dim Command As String = Encoder.RemoteCommand
            Dim Value As String = Encoder.RemoteValue
            If Command = "DirClear" Then RemoteFileList.Clear()
            If Command = "Dir" Then RemoteFileList.Add(Value)

            ListBox1.Items.Clear()
            For Each F As String In RemoteFileList
                ListBox1.Items.Add(F)
            Next
        End If
    End Sub

    Private Sub NetworkClient1_StatusChanged(ByVal Connected As Boolean) Handles NetworkClient1.StatusChanged

    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If NetworkClient1.Connected Then
            Label5.Text = "Connected"
        Else
            Label5.Text = "Disconnected"
        End If
        Label4.Text = NetworkClient1.RemoteIP
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        If NetworkClient1.Connected Then
            Dim F As String = ListBox1.SelectedItem
            NetworkClient1.Send(Encoder.Encode("Load", F))
        End If
    End Sub
End Class
